#!/usr/bin/env bash

source /etc/profile
if [ $# -eq 1 ]; then
	targetHour=$1
else
	targetHour=`date -d "-1 hours" +"%Y%m%d%H"`
fi
path=$(cd $(dirname $0);pwd)
parentPath=$(dirname ${path})
targetDate=${targetHour:0:8}
if [ ! -d "${path}/${targetDate}/${targetHour}" ];then
	mkdir -p ${path}/${targetDate}/${targetHour}
fi

#referer top100
hive -e "set mapred.job.name=[NAS][overview][referer][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}/referer' select cid,sid,${targetDate},1,referer,count(1) pv  from nas_tracking_format_hour where day=${targetDate} and referer <> \"\"  group by cid,sid,referer order by pv desc limit 100;"

#搜索词 top100
hive -e "set mapred.job.name=[NAS][overview][search][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}/search' select cid,sid,${targetDate},5,query,count(1) pv  from nas_tracking_format_hour where day=${targetDate} and query <> \"\"  group by cid,sid,query order by pv  desc limit 100;"

#入口页面 top100
#hive -e "set mapred.job.name=[NAS][overview][entry][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}/entry' select a.cid,a.sid,${targetDate},2,split(minurl,\"\\|\")[1],count(1) pv  from (select cid,sid,session,min(concat_ws(\"|\",from_unixtime(time),formatlasturlslash(ep))) minurl from nas_tracking_format_hour where day=${targetDate} and formatlasturlslash(ep)<>'unknown' group by cid,sid,session) a group by a.cid,a.sid,split(minurl,\"\\|\")[1] order by pv desc limit 100;"
hive -e "add jar ${parentPath}/lib/NASUDF-0.0.1.jar;create temporary function formatlasturlslash as 'com.nsw.udf.FormatLastURLSlash';set mapred.job.name=[NAS][overview][entry][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}/entry' select a.cid,a.sid,${targetDate},2,a.ep,count(1) as pv from (select cid,sid,session,min(struct(time,formatlasturlslash(ep))).col2 as ep from nas_tracking_format_hour where day=${targetDate} and formatlasturlslash(ep)<>'unknown' group by cid,sid,session) a group by a.cid,a.sid,a.ep order by pv desc limit 100"


#受访页面
hive -e "set mapred.job.name=[NAS][overview][visit][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}/visit' select cid,sid,${targetDate},3, ep,count(1) pv  from nas_tracking_format_hour where ep <> \"\" and day=${targetDate}  group by cid,sid,ep order by pv desc limit 100;"

#事件top100
hive -e "set mapred.job.name=[NAS][overview][eventid][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}/eventid' select cid,sid,${targetDate},4,eventid,count(1) pv  from nas_tracking_format_hour where day=${targetDate} and eventid is not NULL  group by cid,sid,eventid order by pv  desc limit 100;"

wait


cat /dev/null > ${path}/${targetDate}/overview_data.txt
cat ${path}/${targetDate}/${targetHour}/referer/* >> ${path}/${targetDate}/overview_data.txt
cat ${path}/${targetDate}/${targetHour}/search/* >> ${path}/${targetDate}/overview_data.txt
cat ${path}/${targetDate}/${targetHour}/entry/* >> ${path}/${targetDate}/overview_data.txt
cat ${path}/${targetDate}/${targetHour}/visit/* >> ${path}/${targetDate}/overview_data.txt
cat ${path}/${targetDate}/${targetHour}/eventid/* >> ${path}/${targetDate}/overview_data.txt
# load data to mysql
source ${parentPath}/configs/mysql_config
python ${path}/Load2Mysql.py ${path}/${targetDate}/overview_data.txt ${host} ${port} ${user} ${password} ${db} ${targetDate}
